package com.projectsexception.festivales.provider.handlers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.projectsexception.festivales.provider.model.Band;
import com.projectsexception.festivales.provider.model.Channel;
import com.projectsexception.festivales.provider.model.OfficialChannel;
import com.projectsexception.festivales.provider.util.SAXHandlerUtil;

public class BandsHandler extends DefaultHandler {
    
    private static final String BAND_ELEMENT = "band";
    private static final String ID_ATTRIBUTE = "id";
    private static final String NAME_ATTRIBUTE = "name";
    private static final String CHANNELS_ELEMENT = "officialChannels";
    private static final String CHANNEL_ELEMENT = "channel";
    private static final String PHOTO_ELEMENT = "photoURL";
    private static final String TYPE_ATTRIBUTE = "type";
    private static final String LINK_ATTRIBUTE = "link";
    
    private Map<String, Band> bands;
    private String defaultPhotoURL;
    
    private transient Band tmpBand;
    private transient boolean data;
    private transient StringBuilder sb;
    
    public BandsHandler(String defaultPhotoURL) {
        this.defaultPhotoURL = defaultPhotoURL;
    }

    @Override
    public void startDocument() {
        bands = new HashMap<String, Band>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        String s = SAXHandlerUtil.getElementName(localName, qName);
        if (s.equals(BAND_ELEMENT)) {
            tmpBand = new Band();            
            tmpBand.setId(attributes.getValue(ID_ATTRIBUTE));
            tmpBand.setName(attributes.getValue(NAME_ATTRIBUTE));
        } else if (s.equals(CHANNELS_ELEMENT)) {
            tmpBand.setOfficialChannels(new ArrayList<OfficialChannel>());
        } else if (s.equals(CHANNEL_ELEMENT)) {
            OfficialChannel channel = new OfficialChannel();
            channel.setType(Channel.valueOf(Channel.class, attributes.getValue(TYPE_ATTRIBUTE)));
            channel.setLink(attributes.getValue(LINK_ATTRIBUTE));
            tmpBand.getOfficialChannels().add(channel);
        } else if (s.equals(PHOTO_ELEMENT)) {
            data = true;
            sb = new StringBuilder();
        }
    }
    
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (data) {
            sb.append(new String(ch, start, length));
        }
    }
    
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        String s = SAXHandlerUtil.getElementName(localName, qName);
        if (s.equals(BAND_ELEMENT)) {
            if (tmpBand.getPhotoURL() == null) {
                tmpBand.setPhotoURL(defaultPhotoURL);
            }
            bands.put(tmpBand.getId(), tmpBand);
        } else if (s.equals(PHOTO_ELEMENT)) {
            tmpBand.setPhotoURL(SAXHandlerUtil.cleanString(sb.toString()));
            data = false;
        }
    }

    public Map<String, Band> getResult() {
        return bands;
    }

}
